package com.boho.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期工具类
 * 
 * @author daiqunquan
 * 
 */
public class DateUtils {
	/**
	 * 获取指定格式当前时间的字符串
	 * 
	 * @param format
	 * @return
	 */
	public static String getCurrentTimeStrByFormat(String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		Date tDate = new Date();
		String dateString = formatter.format(tDate);
		return dateString;
	}

	/**
	 * 获取默认格式当前时间的字符串
	 * 
	 * @return
	 */
	public static String getCurrentTimeStrByFormat() {
		return getCurrentTimeStrByFormat("yyyy-MM-dd");
	}

	/**
	 * 获取默认格式指定时间的字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String getDateTimeStrByFormat(Date date) {
		if(null==date){
			return "";
		}
		return getDateTimeStrByFormat(date,"yyyy-MM-dd");
	}
	
	/**
	 * 获取默认格式指定时间的字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String getDateTimeStrByFormat(String strDate) {
		Date date = new Date(strDate);
		return getDateTimeStrByFormat(date,"yyyy-MM-dd");
	}

	/**
	 * 获取指定格式指定时间的字符串
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDateTimeStrByFormat(Date date, String format) {
		if(null==date){
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 根据当前时间指定格式生成序列号
	 * 
	 * @param format
	 * @return
	 */
	public static String getSecSerialNOByDate(String format) {
		
		return getSecSerialNOByDate(new Date(),format);
	}

	/**
	 * 根据指定时间指定格式生成序列号
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getSecSerialNOByDate(Date date, String format) {
		if(null==date){
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(date);
		return dateString;
	}
	
	/**
	 * 获取距离1970年1月1日0点0分0秒的毫秒数
	 * @return
	 */
	public static String getMSecSerialNoByDate(){
	 	
	 	return String.valueOf(System.currentTimeMillis());

	}
	/**
	 * 获取日期的前一天
	 * @param date
	 * @return
	 */
	public static Date getTheDayBefore(Date date){
		if(null==date){
			return null;
		}
		Calendar cal = Calendar.getInstance();    
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH,-1);      
		return cal.getTime();
	}
	
	/**
	 * 获取日期的前几天
	 * @param date
	 * @return
	 */
	public static Date getTheDayBefore(Date date,int dayCount){
		if(null==date){
			return null;
		}
		Calendar cal = Calendar.getInstance();    
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH,-dayCount);      
		return cal.getTime();
	}
	
	/**
	 * 获取日期的后一天
	 * @param date
	 * @return
	 */
	public static Date getTheDayAfter(Date date){
		if(null==date){
			return null;
		}
		Calendar cal = Calendar.getInstance();    
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH,1);      
		return cal.getTime();
	}
	
	
	
	/**
	 * 获取日期的前一天
	 * @param date
	 * @return
	 */
	public static String getTheDayBefore(Date date,String  format){
		if(null==date){
			return "";
		}
		String time=null;
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();    
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH,-1);      
		time=simpleDateFormat.format(cal.getTime());
		return time;
	}
	
	
	/**
	 * 获取日期的上一个月的日期
	 * @param date
	 * @return
	 */
	public static String getTheMonthBefore(Date date,String  format){
		if(null==date){
			return "";
		}
		String time=null;
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();    
		cal.setTime(date);
		cal.add(Calendar.MONTH,-1);
		time=simpleDateFormat.format(cal.getTime());
		return time;
	}
	
	/**
	 * 获取日期的上一个星期的日期
	 * @param date
	 * @return
	 */
	public static String getTheWeekBefore(Date date,String  format){
		if(null==date){
			return "";
		}
		String time=null;
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();    
		cal.setTime(date);
		cal.add(Calendar.WEEK_OF_YEAR,-1);
		time=simpleDateFormat.format(cal.getTime());
		return time;
	}
	
	
	
	
	 /**
	  * 获取上个月的第一天
	  * @param date
	  * @param format
	  * @return
	  */
	public static String getLastMonthFirstDay(Date date ,String format) {
		if(null==date){
			return "";
		}
	  SimpleDateFormat df = new SimpleDateFormat(format);
	  GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
	  Calendar calendar = Calendar.getInstance();
	  calendar.setTime(date);
	  calendar.add(Calendar.MONTH, -1);
	  Date theDate = calendar.getTime();
	  gcLast.setTime(theDate);
	  gcLast.set(Calendar.DAY_OF_MONTH, 1);
	  String day_first_prevM = df.format(gcLast.getTime());
	  return day_first_prevM;
	 }
	
	/**
	 * 获取上个月的最后一天
	 * @param date
	 * @param format
	 * @return
	 */
	public static  String getLastMonthLastDay(Date date,String format) {
		if(null==date){
			return "";
		}
	  SimpleDateFormat df = new SimpleDateFormat(format);
	  Calendar cal = Calendar.getInstance();
	  Calendar calendar = Calendar.getInstance();
	  calendar.setTime(date);
	  calendar.add(Calendar.MONTH, -1);
	  calendar.add(cal.MONTH, 1);
	  calendar.set(cal.DATE, 1);
	  calendar.add(cal.DATE, -1);
	  String day_end_prevM = df.format(calendar.getTime());
	  return day_end_prevM;
		 }

	/**
	 * 获取当前日期的前N秒的时间
	 * @param date
	 * @return
	 */
	public static Date getSecBefore(int sec){
		Date date = new Date();
		return getSecBefore(date, sec);
	}
	
	/**
	 * 获取指定日期的前N秒的时间
	 * @param date
	 * @return
	 */
	public static Date getSecBefore(Date date,int sec){
		Calendar cal = Calendar.getInstance();    
		cal.setTime(date);
		cal.add(Calendar.SECOND,-sec);      
		return cal.getTime();
	}
	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println(DateUtils.getCurrentTimeStrByFormat());
		System.out.println(sdf.format(DateUtils.getSecBefore(30)));
	}
	/*
	 * 返回两个日期相差的天数
	 * @param startDate 开始日期
	 * @param endDate 结束日期
	 * @return  相差的天数
	 */
	public static long getDisDates(Date startDate,Date endDate){
		long totalDate=0;
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(startDate);
		long timestart=calendar.getTimeInMillis();
		calendar.setTime(endDate);
		long timeend=calendar.getTimeInMillis();
		totalDate=(timeend-timestart)/(1000*60*60*24);
		return totalDate;
	}
	public static long getDisDates(String start,String end){
		Date startDate=null;
		Date endDate=null;
		try{
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			startDate=sdf.parse(start);
			endDate=sdf.parse(end);
		}catch(Exception e){
			e.printStackTrace();
		}
		return getDisDates(startDate,endDate);
	}
}